package tool
{
	import away3d.textures.BitmapTexture;
	
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	
	
	/**
	 * 有时要切下图，就到这里来使用相应的工具
	 */
	public class BitmapTool
	{
		public static function cutImageUseXML(bmd:BitmapData,xml:XML,asBitmapTexture:Boolean=false):Dictionary
		{
			var bmds:Dictionary = new Dictionary;
			
			for each(var ima:XML in xml.children())
			{
				var name:String = ima.name();
				var x:int = int(ima.@x);
				var y:int = int(ima.@y);
				var wid:int = int(ima.@wid);
				var hei:int = int(ima.@hei);
				
				var subbmd:BitmapData = new BitmapData(wid,hei,true,0);
				subbmd.copyPixels(bmd,new Rectangle(x,y,wid,hei),new Point);
				
				if(asBitmapTexture)bmds[name] = new BitmapTexture(subbmd)
				else bmds[name] = subbmd;
			}
			
			return bmds;
		}
		
		public static function cutImageWithSameSize(bmd:BitmapData,Ucount:uint,Vcount:uint,posX:int = 0,posY:int = 0,Width:uint = 0,Height:uint = 0,asBitmapTexture:Boolean = false):Array
		{
			var bmds:Array = new Array;
			
			var wid:uint = bmd.width/Ucount;
			if(Width)wid = Width;
			
			var hei:uint = bmd.height/Vcount;
			if(Height)hei = Height;
			
			for(var v:int=0; v<Vcount;v++)
			{
				for(var u:int=0;u<Ucount;u++)
				{
					var x:int = posX+u*wid;
					var y:int = posY+v*hei;
					
					var subbmd:BitmapData = new BitmapData(wid,hei,true,0);
					subbmd.copyPixels(bmd,new Rectangle(x,y,wid,hei),new Point);
					
					if(asBitmapTexture)bmds.push( new BitmapTexture(subbmd));
					else bmds.push(subbmd);
				}
			}
			 
			return bmds;
		}
	}
}